/*
 * @Descripttion: HN-SaaS管理系统
 * @version: 1.0.0
 * @Author: 侯建军
 * @Date: 2020-09-02 15:38:41
 * @LastEditors: 侯建军
 * @LastEditTime: 2020-09-11 13:54:13
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/localStorage.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/layout/Home.vue'),
    children: [
      {
        path: '/',
        name: 'info',
        component: () => import(/* webpackChunkName: "info" */ '../views/layout/Info.vue')
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import(/* webpackChunkName: "role" */ '../views/role/Role.vue')
      },
      {
        path: 'user',
        name: 'User',
        component: () => import(/* webpackChunkName: "user" */ '../views/user/User.vue')
      },
      {
        path: 'patient',
        name: 'Patient',
        component: () => import(/* webpackChunkName: "patient" */ '../views/patient/Patient.vue')
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import(/* webpackChunkName: "register" */ '../views/register/Register.vue')
      },
      {
        path: 'medicals',
        name: 'Medicals',
        component: () => import(/* webpackChunkName: "register" */ '../views/medicals/Medicals.vue')
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import(/* webpackChunkName: "register" */ '../views/register/Register.vue')
      },
      {
        path: 'resource',
        name: 'Resource',
        component: () => import(/* webpackChunkName: "resource" */ '../views/resource/Resource.vue')
      },
      {
        path: 'inspect',
        name: 'Inspect',
        component: () => import(/* webpackChunkName: "inspect" */ '../views/inspect/Inspect.vue')
      },
      {
        path: 'technology/template',
        name: 'technology_template',
        component: () => import(/* webpackChunkName: "technology_template" */ '../views/inspect/Template.vue')
      },
      {
        path: 'inspectItem',
        name: 'inspectItem',
        component: () => import(/* webpackChunkName: "inspectItem" */ '../views/inspect/InspectItem.vue')
      },
      {
        path: 'medicals/template',
        name: 'medicals_template',
        component: () => import(/* webpackChunkName: "medicals_template" */ '../views/medicals/Template.vue')
      },
      {
        path: 'medicine/chinese',
        name: 'medicine_chinese',
        component: () => import(/* webpackChunkName: "chinese" */ '../views/medicine/Chinese.vue')
      },
      {
        path: 'medicine/western',
        name: 'medicine_western',
        component: () => import(/* webpackChunkName: "western" */ '../views/medicine/Western.vue')
      },
      {
        path: 'report/charge',
        name: 'report_charge',
        component: () => import(/* webpackChunkName: "charge" */ '../views/report/Charge.vue')
      },
      {
        path: 'report/department',
        name: 'report_department',
        component: () => import(/* webpackChunkName: "department" */ '../views/report/Department.vue')
      },
      {
        path: 'report/doctor',
        name: 'report_doctor',
        component: () => import(/* webpackChunkName: "doctor" */ '../views/report/Doctor.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next();
  } else {
    if (getToken() != null) {
      next();
    } else {
      router.push("/login");
    }
  }
});

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => {
    console.log("路由添加重复")
  })
}

export default router
